package com.ogawa.project6602.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.project6602.R
import com.ogawa.project6602.base.Base6602MqttActivity
import com.ogawa.project6602.bean.Massage6602Armchair
import kotlinx.android.synthetic.main.activity_6602_auto.*

@Route(path = UrlPathConstant.ACTIVITY_6602_AUTO)
class AutoProgramActivity : Base6602MqttActivity(), View.OnClickListener {
    private lateinit var tvDashi: TextView
    private lateinit var tvTaishi: TextView
    private lateinit var tvGuangjie: TextView
    private lateinit var tvShujin: TextView
    private lateinit var tvQingchen: TextView
    private lateinit var tvHuoli: TextView
    private lateinit var tvTaiji: TextView
    private lateinit var tvNvwang: TextView
    private lateinit var tvNanshi: TextView
    private lateinit var tvLaonian: TextView
    private lateinit var tvJapanese: TextView
    private lateinit var tvIndian: TextView
    private lateinit var tvParis: TextView
    private lateinit var tvChinese: TextView
    private lateinit var tvBangong: TextView
    private lateinit var tvDitou: TextView
    private lateinit var tvJiache: TextView
    private lateinit var tvGaogen: TextView
    private var tvCurrent: TextView? = null
    private var program: String = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_6602_auto
    }


    override fun initView() {
        super.initView()
        setTitle(getString(R.string.auto_massage))
        tvDashi = findViewById(R.id.tv_dashi)
        tvDashi.setOnClickListener(this)
        tvTaishi = findViewById(R.id.tv_taishi)
        tvTaishi.setOnClickListener(this)
        tvGuangjie = findViewById(R.id.tv_guangjie)
        tvGuangjie.setOnClickListener(this)
        tvShujin = findViewById(R.id.tv_shujin)
        tvShujin.setOnClickListener(this)
        tvQingchen = findViewById(R.id.tv_qingchen)
        tvQingchen.setOnClickListener(this)
        tvHuoli = findViewById(R.id.tv_huoli)
        tvHuoli.setOnClickListener(this)
        tvTaiji = findViewById(R.id.tv_taiji)
        tvTaiji.setOnClickListener(this)
        tvNvwang = findViewById(R.id.tv_nvwang)
        tvNvwang.setOnClickListener(this)
        tvNanshi = findViewById(R.id.tv_nanshi)
        tvNanshi.setOnClickListener(this)
        tvLaonian = findViewById(R.id.tv_laonian)
        tvLaonian.setOnClickListener(this)
        tvJapanese = findViewById(R.id.tv_japanese)
        tvJapanese.setOnClickListener(this)
        tvIndian = findViewById(R.id.tv_indian)
        tvIndian.setOnClickListener(this)
        tvParis = findViewById(R.id.tv_paris)
        tvParis.setOnClickListener(this)
        tvChinese = findViewById(R.id.tv_chinese)
        tvChinese.setOnClickListener(this)
        tvBangong = findViewById(R.id.tv_bangong)
        tvBangong.setOnClickListener(this)
        tvDitou = findViewById(R.id.tv_ditou)
        tvDitou.setOnClickListener(this)
        tvJiache = findViewById(R.id.tv_jiache)
        tvJiache.setOnClickListener(this)
        tvGaogen = findViewById(R.id.tv_gaogen)
        tvGaogen.setOnClickListener(this)
        tv_yuanqi.setOnClickListener(this)
        tv_pinghen.setOnClickListener(this)
        tv_shenceng.setOnClickListener(this)
        tv_jiuyang.setOnClickListener(this)
        tv_xianglong.setOnClickListener(this)
        tv_liumai.setOnClickListener(this)
        tv_yiyang.setOnClickListener(this)
        tv_taijiquan.setOnClickListener(this)
        tv_qiankun.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_cancel).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        setProgram()
    }

    private fun setProgram() {
        val deviceStatusBean = Massage6602Armchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val auto = Integer.parseInt(deviceStatusBean.functions.autoProgram.modelValue)
            tvDashi.isSelected = auto == 1
            tvTaishi.isSelected = auto == 2
            tvGuangjie.isSelected = auto == 3
            tvShujin.isSelected = auto == 4
            tv_yuanqi.isSelected = auto == 5
            tv_pinghen.isSelected = auto == 6
            tv_shenceng.isSelected = auto == 7
            tvQingchen.isSelected = auto == 8
            tvHuoli.isSelected = auto == 9
            tvTaiji.isSelected = auto == 10
            tvNvwang.isSelected = auto == 11
            tvNanshi.isSelected = auto == 12
            tvLaonian.isSelected = auto == 13
            tvJapanese.isSelected = auto == 14
            tvIndian.isSelected = auto == 15
            tvParis.isSelected = auto == 16
            tvChinese.isSelected = auto == 17
            tvBangong.isSelected = auto == 18
            tvDitou.isSelected = auto == 19
            tvJiache.isSelected = auto == 20
            tvGaogen.isSelected = auto == 21
            tv_jiuyang.isSelected = auto == 22
            tv_xianglong.isSelected = auto == 23
            tv_liumai.isSelected = auto == 24
            tv_yiyang.isSelected = auto == 25
            tv_taijiquan.isSelected = auto == 26
            tv_qiankun.isSelected = auto == 27
            when (auto) {
                1 -> tvCurrent = tvDashi
                2 -> tvCurrent = tvTaishi
                3 -> tvCurrent = tvGuangjie
                4 -> tvCurrent = tvShujin
                5 -> tvCurrent = tv_yuanqi
                6 -> tvCurrent = tv_pinghen
                7 -> tvCurrent = tv_shenceng
                8 -> tvCurrent = tvQingchen
                9 -> tvCurrent = tvHuoli
                10 -> tvCurrent = tvTaiji
                11 -> tvCurrent = tvNvwang
                12 -> tvCurrent = tvNanshi
                13 -> tvCurrent = tvLaonian
                14 -> tvCurrent = tvJapanese
                15 -> tvCurrent = tvIndian
                16 -> tvCurrent = tvParis
                17 -> tvCurrent = tvChinese
                18 -> tvCurrent = tvBangong
                19 -> tvCurrent = tvDitou
                20 -> tvCurrent = tvJiache
                21 -> tvCurrent = tvGaogen
                22 -> tvCurrent = tv_jiuyang
                23 -> tvCurrent = tv_xianglong
                24 -> tvCurrent = tv_liumai
                25 -> tvCurrent = tv_yiyang
                26 -> tvCurrent = tv_taijiquan
                27 -> tvCurrent = tv_qiankun
            }
        }
    }

    override fun onClick(v: View?) {
        if (v != tvCurrent) {
            v?.isSelected = true
            tvCurrent?.isSelected = false
            tvCurrent = v as TextView?
        }
        when (v?.id) {
            R.id.tv_dashi -> program = "1"
            R.id.tv_taishi -> program = "2"
            R.id.tv_guangjie -> program = "3"
            R.id.tv_shujin -> program = "4"
            R.id.tv_yuanqi -> program = "5"
            R.id.tv_pinghen -> program = "6"
            R.id.tv_shenceng -> program = "7"
            R.id.tv_qingchen -> program = "8"
            R.id.tv_huoli -> program = "9"
            R.id.tv_taiji -> program = "10"
            R.id.tv_nvwang -> program = "11"
            R.id.tv_nanshi -> program = "12"
            R.id.tv_laonian -> program = "13"
            R.id.tv_japanese -> program = "14"
            R.id.tv_indian -> program = "15"
            R.id.tv_paris -> program = "16"
            R.id.tv_chinese -> program = "17"
            R.id.tv_bangong -> program = "18"
            R.id.tv_ditou -> program = "19"
            R.id.tv_jiache -> program = "20"
            R.id.tv_gaogen -> program = "21"
            R.id.tv_jiuyang -> program = "22"
            R.id.tv_xianglong -> program = "23"
            R.id.tv_liumai -> program = "24"
            R.id.tv_yiyang -> program = "25"
            R.id.tv_taijiquan -> program = "26"
            R.id.tv_qiankun -> program = "27"
            R.id.tv_cancel -> {
//                program = ""
                finish()
            }
            R.id.tv_confirm -> {
                if (program.isNotEmpty()) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("program", program))
                }
                finish()
            }
        }
    }

}